package com.linzm.leetcode.primary.exercises1_20230114;

import java.util.Arrays;

/**
 * @Author zimingl
 * @Date 2023/1/15 14:47
 * @Description: 基于排列构建数组
 */
public class Demo16_1920 {
    /**
     * 输入：nums = [0,2,1,5,3,4]
     * 输出：[0,1,2,4,5,3]
     * 解释：数组 ans 构建如下：
     * ans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]], nums[nums[5]]]
     *     = [nums[0], nums[2], nums[1], nums[5], nums[3], nums[4]]
     *     = [0,1,2,4,5,3]
     */
    public static void main(String[] args) {
        Demo16_1920 demo16_1920 = new Demo16_1920();
        int[] nums = {5,0,1,2,3,4};
//        int[] nums = {0,2,1,5,3,4};
        int[] resNums = demo16_1920.buildArray(nums);
        String[] strings = Arrays.stream(resNums).mapToObj(String::valueOf).toArray(String[]::new);
        System.out.println(String.join(" ", strings));
    }

    private int[] buildArray(int[] nums) {
        int[] arr = new int[nums.length];
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < nums.length; j++) {
                if (nums[i] == j) {
                    arr[i] = nums[j];
                }
            }
        }
        return arr;
    }

    private int[] buildArray2(int[] nums) {
        int[] arr = new int[nums.length];
        for (int i = 0; i < nums.length; i++) {
            arr[i] = nums[nums[i]];
        }
        return arr;
    }
}
